package io.xccit.aischool.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import io.xccit.aischool.entity.Grade;
import io.xccit.aischool.service.IGradeService;
import io.xccit.aischool.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author CH_ywx
 * @date 2023-06-06
 * @description
 */
@Api(tags = {"年级接口"})
@RestController
@RequestMapping("/sms/gradeController")
public class GradeController {
    @Autowired
    private IGradeService gradeService;

    @ApiOperation("所有年级查询")
    @GetMapping("/getGrades")
    public Result getAllGrades(){
        List<Grade> grades = gradeService.getAllGrades();
        return Result.ok(grades);
    }

    /**
     * 分页查询年级
     * @param pageNo
     * @param pageSize
     * @param gradeName
     * @return
     */
    @ApiOperation("年级查询")
    @GetMapping("/getGrades/{pageNo}/{pageSize}")
    public Result getGrades(@ApiParam("页数") @PathVariable Integer pageNo,
                            @ApiParam("每页条数") @PathVariable Integer pageSize,
                            @ApiParam("年级名称-模糊查询用(可留空)") @RequestParam @Nullable String gradeName){
        Page<Grade> gradePage = new Page<>(pageNo, pageSize);
        IPage gradesByOpr = gradeService.getGradesByOpr(gradePage, gradeName);
        return Result.ok(gradesByOpr);
    }

    /**
     * 添加或修改年级 有id为修改,没有id为添加
     * @param grade
     * @return
     */
    @ApiOperation("添加或更新年级")
    @PostMapping("/saveOrUpdateGrade")
    public Result saveOrUpdate(@ApiParam("年级请求体-有id为更新,没id为添加") @RequestBody Grade grade){
        //直接调用service层方法自行判断
       return Result.ok(gradeService.saveOrUpdate(grade));
    }

    /**
     * 删除/批量删除
     * @param ids id列表
     * @return
     */
    @ApiOperation("删除年级")
    @DeleteMapping("/deleteGrade")
    public Result deleteGrade(@ApiParam("要删除年级的id-多个为批量删除") @RequestBody List<Integer> ids){
        gradeService.removeByIds(ids);
        return Result.ok();
    }
}
